package com.simin.base.appdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.LayoutRes;

import com.simin.base.R;


/**
 * date:2023/9/27
 * author:wsm
 * faucation:PopupWindow管理
 */
public class PopupWindowManager {

    /**
     * show弹窗popupwindow
     *
     * @param activity           上下文
     * @param layoutId           res布局文件
     * @param onBindViewCallback bind视图的回调
     * @return dialog
     */
    public static PopupWindow buildBottomSheetDialog(Activity activity, @LayoutRes int layoutId, View v, OnBindViewCallback onBindViewCallback) {
        return buildBottomSheetDialog(activity, layoutId, v, LinearLayout.LayoutParams.WRAP_CONTENT, onBindViewCallback);
    }

    /**
     * show弹窗popupwindow
     *
     * @param activity           上下文
     * @param layoutId           res布局文件
     * @param onBindViewCallback bind视图的回调
     * @return dialog
     */
    public static PopupWindow buildBottomSheetDialog(Activity activity, @LayoutRes int layoutId, View v, int width, OnBindViewCallback onBindViewCallback) {
        View view = LayoutInflater.from(activity).inflate(layoutId, null);
        PopupWindow popupWindow = new PopupWindow(view, width, LinearLayout.LayoutParams.WRAP_CONTENT);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = view.getMeasuredWidth();
        int popupHeight = view.getMeasuredHeight();
        int[] location = new int[2];
        // 允许点击外部消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        // 获得位置
        v.getLocationOnScreen(location);
        //上
//        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
        //下
        popupWindow.showAsDropDown(v);
        //左
//        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
        //右
        // popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
        //背景颜色1f是白色
//        setBackgroundAlpha(activity, 0.7f);
        //内容显示操作
        if (onBindViewCallback != null) {
            onBindViewCallback.onBindView(view, popupWindow);
        }
        //点击外部消失
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
//                setBackgroundAlpha(activity, 1f);
            }
        });
        return popupWindow;
    }


    /**
     * 权限申请弹窗说明
     *
     * @param activity
     * @return
     */
    public static PopupWindow showPermissionApply(Activity activity, String title, String content) {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_permission_apply_layout, null);
        PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 允许点击外部消失
        popupWindow.setBackgroundDrawable(null);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(false);
        //上弹出
        //背景颜色1f是白色
//        setBackgroundAlpha(activity, 0.7f);
        //内容显示操作
        TextView titleTv = view.findViewById(R.id.title_tv);
        TextView contentTv = view.findViewById(R.id.content_tv);
        titleTv.setText(title);
        contentTv.setText(content);
        if (!activity.isFinishing() && !activity.isDestroyed()) {
            popupWindow.showAtLocation(view, Gravity.TOP, 0, 0);
        }
        return popupWindow;
    }

    public interface OnBindViewCallback {
        void onBindView(View view, PopupWindow popupWindow);
    }

}
